<template>
  <TopNav title="智能生成个性营养计划"/>
  <div class="container">
    <!--顶部 start-->
    <div class="top">
      <el-progress class="score-in"
                   type="circle"
                   color="#3ddadd"
                   :indeterminate="true"
                   :percentage="process"
                   :stroke-width="10"
                   width="200"
      >
        <template #default="{ percentage }">
          <img
              src="">
        </template>
      </el-progress>
    </div>
    <!--顶部 end-->
    <!--按钮 start-->
    <el-button @click="createPlanHandle" v-if="process<20" class="button" round color="#3ddadd"><span>开始生成</span>
    </el-button>
    <el-button v-else-if="process<100" class="button" round color="#3ddadd"><span>正在生成中...</span></el-button>
    <el-button v-else @click="router.push('/plan/nutritionPlan/index/'+route.params.name)" class="button" round
               color="#409eff"><span>去查看</span></el-button>
    <!--按钮 end-->
  </div>
</template>

<script setup>

import {useDataStore} from "@/stores/dataStore.js";
import {useRoute, useRouter} from 'vue-router'
import TopNav from "@/components/TopNav/Back.vue"
import {onMounted, ref} from "vue";
import patientAxios from "@/api/patient.js"
import langchain4jAxios from "@/api2/langchain4j.js"

const route = useRoute()
const router = useRouter()
const dataStore = useDataStore()
//评估进度
const process = ref(0)
onMounted(() => {
})
/**
 * 生成营养计划
 */
const createPlanHandle = () => {
  console.log("1.开始生成营养计划....")
  process.value = 20
  //5秒后执行
  new Promise(resolve => setTimeout(http_createPlan, 5000));
}
/**
 * 生成营养计划请求【外置ai版】
 */
const patient_text = ref('')
const http_createPlan = () => {
  console.log("2.发送生成计划请求....")
  process.value = 40
  http_patient_findById()
}
//1.查询患者，获取患者信息，制作ai提示词
const http_patient_findById = () => {
  patientAxios.patient_findById({
    patientId: route.params.name
  }).then(res => {
    if (res.data.code == 200) {
      console.log("3.获取患者信息....")
      process.value = 60
      patient_text.value = "根据以下患者信息"
          + "，患者的性别为：" + res.data.data.patientSex
          + "，患者的年龄为：" + res.data.data.patientAge
          + "，患者的身高为：" + res.data.data.height
          + "，患者的体重为：" + res.data.data.weight
          + "，患者所患疾病为：" + res.data.data.disease
      http_ai_chat_NutritionPlan()
    } else {
      ElMessage.error(res.data.data.message)
    }
  })
}
//2.访问ai，生成营养计划、膳食方案、肠内营养方案、肠外营养方案
const http_ai_chat_NutritionPlan = () => {
  langchain4jAxios.base_patientNutritionPlan({
    message: patient_text.value
  }).then(res => {
    if (res.data.code == 200) {
      console.log("4.访问ai，生成营养计划、膳食方案、肠内营养方案、肠外营养方案....")
      process.value = 80
      patientAxios.patient_generatePlanAiForNutritionPlan({
        planName: res.data.data.planName,
        nutritionPlanExecutionStatus: res.data.data.nutritionPlanExecutionStatus,
        goal: res.data.data.goal,
        dailyCalories: res.data.data.dailyCalories,
        dailyProtein: res.data.data.dailyProtein,
        dailyCarbohydrates: res.data.data.dailyCarbohydrates,
        dailyFat: res.data.data.dailyFat,
        patientId: route.params.name
      }).then(res => {
        if (res.data.code == 200) {
          process.value = 80
          console.log("nutritionPlanId:" + res.data.data)//nutritionPlanId
          http_ai_chat_MealPlan(res.data.data)
          http_ai_chat_enteralNutritionPlan(res.data.data)
          http_ai_chat_ParenteralNutritionPlan(res.data.data)
        }
      })
    }
  })
}
const http_ai_chat_MealPlan = (nutritionPlanId) => {
  langchain4jAxios.base_patientMealPlan({
    message: patient_text.value
  }).then(res => {
    if (res.data.code == 200) {
      console.log("5.将数据保存到数据库....")
      process.value = 100
      //console.log("膳食计划制定")
      //console.log(res.data)
      patientAxios.patient_generatePlanAiForMealPlan({
        mealType: res.data.data.mealType,
        mealFoods: res.data.data.mealFoods,
        nutritionPlanId: nutritionPlanId
      }).then(res => {
        if (res.data.code == 200) {
          //console.log("膳食计划放入数据库")
        }
      })
    }
  })
}
const http_ai_chat_enteralNutritionPlan = (nutritionPlanId) => {
  langchain4jAxios.base_patientEnteralNutritionPlan({
    message: patient_text.value
  }).then(res => {
    if (res.data.code == 200) {
      //console.log("肠内计划制定")
      //console.log(res.data)
      patientAxios.patient_generatePlanAiForEnteralNutritionPlan({
        formulaType: res.data.data.formulaType,
        volume: res.data.data.volume,
        nutritionPlanId: nutritionPlanId
      }).then(res => {
        if (res.data.code == 200) {
          //console.log("肠内计划放入数据库")
        }
      })
    }
  })
}
const http_ai_chat_ParenteralNutritionPlan = (nutritionPlanId) => {
  langchain4jAxios.base_patientParenteralNutritionPlan({
    message: patient_text.value
  }).then(res => {
    if (res.data.code == 200) {
      //console.log("肠外计划制定")
      //console.log(res.data)
      patientAxios.patient_generatePlanAiForParenteralNutritionPlan({
        nutrientComponents: res.data.data.nutrientComponents,
        infusionRate: res.data.data.infusionRate,
        nutritionPlanId: nutritionPlanId
      }).then(res => {
        if (res.data.code == 200) {
          //console.log("肠外计划放入数据库")
        }
      })
    }
  })
}
</script>

<style scoped lang="scss">
.container {

}

.top {
  background-color: #3ddadd;
  border-radius: 0 0 30% 30%;
  height: 300px;

  .score-in {
    position: fixed;
    margin-top: 30px;
    margin-left: 25%;

    img {
      width: 50px;
    }

    p {
      font-size: 30px;
      color: #409eff;

      span {
        font-size: 15px;
        font-family: 楷体;
        color: #de4848;
      }
    }
  }
}

.button {
  margin-top: 20px;
  margin-left: 10%;
  width: 80%;
  height: 45px;

  span {
    font-weight: bolder;
    font-size: 18px;
    color: #ffffff;
  }
}
</style>
